package com.sls.web.manager.action.recharge;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.Operation;
import com.sls.web.manager.recharge.pojo.PhoneType;
import com.sls.web.manager.recharge.pojo.SystemProduct;
import com.sls.web.manager.recharge.querybean.SystemProductQuery;
import com.sls.web.manager.recharge.service.OperationService;
import com.sls.web.manager.recharge.service.PhoneTypeService;
import com.sls.web.manager.recharge.service.SystemProductService;

/**
 * 系统产品维护
 * 
 * @author sxf
 * 
 */
public class SystemProductAction extends SuperAction {
	private static final long serialVersionUID = -1336381888803546604L;

	@Resource(name = "systemProductService")
	private SystemProductService systemProductService;
	@Resource(name = "operationService")
	private OperationService operationService;
	@Resource(name = "phoneTypeService")
	private PhoneTypeService phoneTypeService;
	private SystemProduct product;
	// 添加库存产品页面
	private List<Operation> operationList;
	private String strPrice;
	// 修改库存产品页面
	private String id;
	private List<SystemProduct> spList;

	private SystemProductQuery query;
	List<PhoneType> phoTypeList;
	Pagination<SystemProduct> page;

	public String list() {

		try {
			spList = systemProductService
					.findRechargeList(getCurrentOperator());
			return "list";
		} catch (Exception e) {
			addActionError(e.getMessage());
			return "list";
		}
	}

	/**
	 * 查询卡密产品
	 * 
	 * @return
	 */
	public String listCardInfo() {
		try {
			if (query == null) {
				query = new SystemProductQuery();
				query.setCurrentPage(Constants.INIT_CURRENTPAGE);
				query.setPageSize(Constants.INIT_PAGESIZE);
			}
			query.setProductType(Constants.SYSTEM_PRODUCT_CARD);
			page = systemProductService.queryPage(query, getCurrentOperator());
		} catch (Exception e) {
			addActionError(e.getMessage());
		}
		return "list_cardinfo";
	}

	public String addRecharge() {
		try {
			if (product == null) {
				operationList = operationService.findList(getCurrentOperator());
				phoTypeList = phoneTypeService.listEnable(getCurrentOperator());
				return "add";
			} else {
				product.setProductType(Constants.SYSTEM_PRODUCT_RECHARGE);
				product.setFaceValue(Constants.SYSTEM_PRODUCT_FACE_VALUE);
				product.setCostPrice(Constants.SYSTEM_PRODUCT_COST_PRICE);
				product.setDoorPrice(Constants.SYSTEM_PRODUCT_DOOR_PRICE);
				product.setDoorAddPrice(Constants.SYSTEM_PRODUCT_DOOR_ADD_PRICE);
				product.setChannelAgentPrice(Constants.SYSTEM_PRODUCT_CHANGE_PRICE);
				systemProductService.insertRecharge(product,
						getCurrentOperator());
				redirectionUrl = "system_product!list.htm";

				return "success";
			}
		} catch (Exception e) {
			addActionError(e.getMessage());
			return "add";
		}

	}

	public String editRecharge() {
		try {
			if (product == null) {
				product = systemProductService.findRechargeById(id,
						getCurrentOperator());
				operationList = operationService.findList(getCurrentOperator());
				phoTypeList = phoneTypeService.listEnable(getCurrentOperator());
				return "add";
			} else {
				systemProductService.updateRecharge(product,
						getCurrentOperator());
			}
			redirectionUrl = "system_product!list.htm";

			return "success";
		} catch (Exception e) {
			addActionError(e.getMessage());
			return "add";
		}
	}

	/**
	 * 卡密产品
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toAdd() throws Exception {
		product = new SystemProduct();
		operationList = operationService.findList(getCurrentOperator());
		if (operationList != null && !operationList.isEmpty()) {
			for (int i = 0; i < operationList.size(); i++) {
				Operation op = operationList.get(i);
				if (op.getEnable() != null && !op.getEnable()) {
					// 过滤无效的运营商
					operationList.remove(i);
				}
			}
		}
		phoTypeList = phoneTypeService.listEnable(getCurrentOperator());
		// StockSupplierQuery ssq = new StockSupplierQuery();
		// stockSuppliersList = stockSupplierService.queryList(ssq,
		// getCurrentOperator());

		return "edit";
	}

	/**
	 * 卡密产品
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doAdd() throws Exception {
		if (product != null) {
			if (StringUtils.isNotBlank(strPrice)) {
				try {
					product.setFaceValue((int) (Double.parseDouble(strPrice
							.trim()) * 1000));
				} catch (Exception e) {

				}
			}
			product.setEnable(true);
			product.setDeleted(false);
			product.setProductType(0);
			product.setUpdateOperator(getCurrentOperator().getUserId());
			systemProductService.insert(product, getCurrentOperator());
		}

		redirectionUrl = "card_management!list.htm";
		return SUCCESS;
	}

	/**
	 * 卡密产品编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		if (StringUtils.isNotBlank(id)) {
			SystemProductQuery spq = new SystemProductQuery();
			spq.setId(id);
			product = systemProductService.queryOne(spq, getCurrentOperator());
		}
		operationList = operationService.findList(getCurrentOperator());
		phoTypeList = phoneTypeService.listEnable(getCurrentOperator());
		if (operationList != null && !operationList.isEmpty()) {
			for (int i = 0; i < operationList.size(); i++) {
				Operation op = operationList.get(i);
				if (op.getEnable() != null && !op.getEnable()) {
					// 过滤无效的运营商
					operationList.remove(i);
				}
			}
		}
		return "edit";
	}

	/**
	 * 卡密产品编辑
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doEdit() throws Exception {
		if (product != null && StringUtils.isNotBlank(product.getId())) {
			if (StringUtils.isNotBlank(strPrice)) {
				try {
					product.setFaceValue((int) (Double.parseDouble(strPrice
							.trim()) * 1000));
				} catch (Exception e) {

				}
			}
			product.setEnable(true);
			product.setDeleted(false);
			product.setProductType(0);
			product.setUpdateOperator(getCurrentOperator().getUserId());
			systemProductService.update(product, getCurrentOperator());
		}

		redirectionUrl = "card_management!list.htm";
		return SUCCESS;
	}

	/**
	 * 检查产品名称是否存在
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkProductName() throws Exception {
		message = "true";
		if (product != null && StringUtils.isNotBlank(product.getName())) {
			SystemProductQuery spq = new SystemProductQuery();
			spq.setName(product.getName());
			SystemProduct sp = systemProductService.queryOne(spq,
					getCurrentOperator());
			if (sp != null) {
				message = "false";
			}
		}
		return "ajax";
	}

	public SystemProduct getProduct() {
		return product;
	}

	public void setProduct(SystemProduct product) {
		this.product = product;
	}

	public List<Operation> getOperationList() {
		return operationList;
	}

	public void setOperationList(List<Operation> operationList) {
		this.operationList = operationList;
	}

	public String getStrPrice() {
		return strPrice;
	}

	public void setStrPrice(String strPrice) {
		this.strPrice = strPrice;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<PhoneType> getPhoTypeList() {
		return phoTypeList;
	}

	public void setPhoTypeList(List<PhoneType> phoTypeList) {
		this.phoTypeList = phoTypeList;
	}

	public Pagination<SystemProduct> getPage() {
		return page;
	}

	public void setPage(Pagination<SystemProduct> page) {
		this.page = page;
	}

	public List<SystemProduct> getSpList() {
		return spList;
	}

	public void setSpList(List<SystemProduct> spList) {
		this.spList = spList;
	}

	public SystemProductQuery getQuery() {
		return query;
	}

	public void setQuery(SystemProductQuery query) {
		this.query = query;
	}

}
